package com.luo.shop.controller;

import com.luo.shop.dao.CartDao;
import com.luo.shop.dao.GoodsDao;
import com.luo.shop.pojo.Cart;
import com.luo.shop.pojo.Goods;
import com.luo.shop.pojo.User;
import com.luo.shop.service.CartService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsDao goodsDao;

    /**
     * 判断用户是否购物了此商品
     * @param gid
     * @param session
     * @return
     */
    @GetMapping("isPayment")
    public ResponseEntity<Boolean> isPayment(Long gid, HttpSession session){
        User user  = (User) session.getAttribute("portalUser");
        if (user.getCartList()==null){
            return ResponseEntity.ok(false);
        }
        for (Cart cart : user.getCartList()) {
            if (cart.getGid().equals(gid) && cart.getIsPayment()){
                return ResponseEntity.ok(true);
            }
        }
        return ResponseEntity.ok(false);
    }

    /**
     * 添加购物车信息
     * @param cart
     * @param session
     * @return
     */
    @PostMapping("save")
    public  ResponseEntity<String> save(Cart cart,HttpSession session){
        User user  = (User) session.getAttribute("portalUser");
        cart.setUid(user.getId());
        Goods goods = goodsDao.selectByPrimaryKey(cart.getGid());
        if (cart.getOrderNumber()>goods.getRepertory()){
            return ResponseEntity.ok("error");
        }
        Integer save = cartService.save(cart);
        if (save>0){
            return ResponseEntity.ok(save+"");
        }
        return ResponseEntity.ok("error");
    }

    /**
     * 成功支付商品
     * @param id
     * @return
     */
    @PostMapping("del")
    public  ResponseEntity<String> del(String id,HttpSession session){
        User user  = (User) session.getAttribute("portalUser");
        Integer del = cartService.del(id,user.getId());
        if (del>0){
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok("error");
    }

    /**
     * 修改购物车信息
     * @param cart
     * @return
     */
    @PostMapping("upd")
    public ResponseEntity<String> upd(Cart cart){
        Integer len = cartService.upd(cart);
        if (len>0){
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok("error");
    }

    /**
     * 删除购物车信息
     * @param id
     * @return
     */
    @GetMapping("delete")
    public ResponseEntity<String> delete(String id){
        Integer len = cartService.delete(id);
        if (len>0){
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok("error");
    }

}
